#include "StdAfx.h"
//#include ".\media_player.h"
#include "mediaplayer.h"
#include <math.h>
#include "player.h"
#include "mplayerwrapper.h"
#include <STDLIB.H>

//marco define
#define OVERLAYCOLOR 0x10
#define VIDEO_WINDOW_CLASS "media_PlayerVideoWindow"


//global var
int s_bgcolor = 0x10;
extern CMPlayer *player;
extern int thread_exit_flag;
extern HINSTANCE hDllInst;
HWND hWnd = NULL;
HWND s_hWnd = NULL;
HWND h_wnd = NULL;

//mfc message callback
LRESULT CALLBACK VideoWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static RECT rc;
	static int mx = 0;
	static int my = 0;
	int x, y;
	RECT rc2;
	char *result = NULL;

	switch (msg) {
case WM_LBUTTONDBLCLK:
	//player[active].Command("vo_fullscreen 1");

	player->Command("get_vo_fullscreen", &result);
	if (!result) {
		break;
	}
	if (*result == 0x30 ) {
		player->Command("vo_fullscreen 1");
	}
	else {
		player->Command("vo_fullscreen 0");
	}

	break;
case WM_MOUSEMOVE:
	break;
	x = LOWORD(lParam);
	y = HIWORD(lParam);
	if (wParam == MK_LBUTTON) {
		SetCursor(LoadCursor(0, MAKEINTRESOURCE(IDC_ARROW)));
		if (!mx) {
			mx = x;
			my = y;
			return false;
		}
	} else {
		mx = 0;
		my = 0;
		return false;
	}
	GetWindowRect(hWnd, &rc);
	GetWindowRect(s_hWnd, &rc2);
	rc.left += x - mx;
	rc.top += y - my;
	rc.right += x - mx;
	rc.bottom += y - my;
	x = rc.left - rc2.left;
	y = rc.top - rc2.top;
	if (x > - 5 && x < 5) x = 0;
	if (y > -5 && y < 5) y = 0;
	MoveWindow(hWnd, x, y, rc.right -rc.left, rc.bottom - rc.top, TRUE);
	break;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
	break;
default:
	return DefWindowProc(hWnd, msg, wParam, lParam);
	}
	return 0;
}

HWND CreateVideoWindow(HWND hwndParent)
{
	WNDCLASS wc;
	ZeroMemory(&wc,sizeof(WNDCLASS));
	wc.style = CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS ;
	wc.lpfnWndProc = (WNDPROC)VideoWindowProc;
	wc.hInstance = hDllInst; //AfxGetApp()->m_hInstance;
	wc.hbrBackground =  CreateSolidBrush(s_bgcolor);
	wc.lpszClassName = VIDEO_WINDOW_CLASS;
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	UnregisterClass(wc.lpszClassName, hDllInst);;
	RegisterClass(&wc);
	return CreateWindow(VIDEO_WINDOW_CLASS,NULL,WS_CHILD | WS_VISIBLE,0,0,100,100,hwndParent,NULL,hDllInst,0);
}

MEDIAPLAYER_API void mp_initial(HWND parent)
{
	RECT rc;
	s_hWnd = parent;
	h_wnd = CreateVideoWindow(s_hWnd);
	::GetClientRect(s_hWnd, &rc);
#if 0
	::MoveWindow(h_wnd, rc.left+20, rc.top+30, rc.right - rc.left-40, rc.bottom - rc.top-60, TRUE);
#else
	::MoveWindow(h_wnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, TRUE);
#endif
	::ShowWindow(h_wnd, SW_SHOW);
}

MEDIAPLAYER_API void mp_start_player(const char *file_name)
{
	char *tmp;
	char path[1024];
	::GetModuleFileName(NULL, path, 512);
	tmp = strrchr(path, '\\');
	*tmp = '\0';

	strcat(path, "\\codec\\mplayer.exe");


	StartPlayer(path, (char *)file_name, 0, s_bgcolor, h_wnd);
}

MEDIAPLAYER_API int mp_get_player_pos() 
{
	int pos;
	pos = mcPlayerGetPos()/1000;

	if (thread_exit_flag == 1) {
		delete player;
		player = NULL;
		return 0;
	}
	return pos;
}


MEDIAPLAYER_API float mp_percent_pos()
{
	char *result = NULL;
	float percent;

	if (!player) {
		return 0.0;
	}

	player->Command("get_time_pos", &result);

	if (!result) {
		return 0.0;
	}
	percent = atof(result);
	return percent;
}

MEDIAPLAYER_API void mp_forward()
{
	char *result = NULL;

	player->Command("get_percent_pos", &result);
	mcPlayerSeek(atoi(result)+1, 1);
}

MEDIAPLAYER_API void mp_seek(int secs)
{
	mcPlayerSeek(secs, 2);
}


MEDIAPLAYER_API void mp_backward()
{
	char *result = NULL;
	int percent;
	player->Command("get_percent_pos", &result);
	percent = atoi(result);
	if (percent > 1) {
		percent -= 1;
	}

	mcPlayerSeek(percent, 1);
}




MEDIAPLAYER_API void mp_inc_volume()
{
	char *result;
	player->Command("volume +1", &result );
}

MEDIAPLAYER_API void mp_dec_volume()
{
	char *result;
	player->Command("volume -1", &result );
}


MEDIAPLAYER_API MP_STATE mp_play_or_pause()
{
	int state;
	mcPlayerPause();
	state = mcPlayerGetState();
	if (state == 2) {
//		GetDlgItem(IDC_BUTTON5)->SetWindowText("Play");
		return PLAYER_STATE_PAUSE;
	}
	else if (state == 1) {
//		GetDlgItem(IDC_BUTTON5)->SetWindowText("Pause");
		return PLAYER_STATE_PLAY;
	}
	return NONE;
}

MEDIAPLAYER_API void mp_stop()
{
//	if (!player) {
//		GetDlgItem(IDC_BUTTON6)->SetWindowText("Stop");
//		StartPlayer(".\\mplayer\\mplayer.exe", "c:\\264.mp4", 0, s_bgcolor, h_wnd);
//		SetTimer(1001, 1000, 0);
//	}
//	else {
//		KillTimer(1001);
		mcPlayerStop();
		if (player) {
			delete player;
			player = NULL;
		}
//		GetDlgItem(IDC_BUTTON6)->SetWindowText("Start");
//	}
}


MEDIAPLAYER_API void mp_release()
{
//	KillTimer(1001);
	mcPlayerStop();
	if (player) {
		delete player;
		player = NULL;
	}
}








